//
//  SN_ReadMore_TableHeaderView.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/19.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ReadMore_TableHeaderView.h"

@implementation SN_ReadMore_TableHeaderView

- (BOOL)canBecomeFirstResponder {
    
    return YES;
}

- (instancetype)init {
    
    return [self initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self) {
        
        self = [super initWithFrame:frame];
        self.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 300, 16)];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_titleLabel];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 15, 0.5)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [self addSubview:_lineView];

        //更多的按钮
        _tableStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_tableStateBtn setTitle:@"展开" forState:UIControlStateNormal];
        [_tableStateBtn setTitle:@"收起" forState:UIControlStateSelected];
        [_tableStateBtn setImage:[UIImage imageNamed:@"展开"] forState:UIControlStateNormal];
        [_tableStateBtn setImage:[UIImage imageNamed:@"收起"] forState:UIControlStateSelected];
        _tableStateBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_tableStateBtn setTitleColor:[UIColor colorWithHexString:@"#bebebe"] forState:UIControlStateNormal];
        _tableStateBtn.frame = CGRectMake(SCREENWIDTH - 15 - 38, 14, 38, 13);
        _tableStateBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_tableStateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
        [_tableStateBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 26, 0, 0)];
        [_tableStateBtn addTarget:self action:@selector(openOrCloseSection:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_tableStateBtn];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openOrCloseSection:)];
        [self addGestureRecognizer:tap];
    }
    
    return self;
}

#pragma mark -Action
- (void)openOrCloseSection: (UITapGestureRecognizer *)tap {

    _tableStateBtn.selected = !_tableStateBtn.selected;
    
    if (_sectionModel.isSectionOpen) { //目前打开状态   目标关闭
        
        if (_delegate && [_delegate respondsToSelector:@selector(closeActionWithSection:)]) {
            
            [_delegate closeActionWithSection:_section];
        }
        
    }else { //目前关闭状态  目标打开
        
        if (_delegate && [_delegate respondsToSelector:@selector(openActionWithSection:)]) {
            
            [_delegate openActionWithSection:_section];
        }
    }
}


#pragma mark -Setter
- (void)setSectionModel:(SN_QuesList *)sectionModel {
    
    _sectionModel = sectionModel;
    
    
    _tableStateBtn.selected = sectionModel.isSectionOpen;
    
    _titleLabel.text = [sectionModel.year stringByAppendingString:@"学年度"];
}

@end
